package com.lyl.common.validation.validator;

import com.lyl.common.validation.annotation.FileNotEmpty;
import org.springframework.web.multipart.MultipartHttpServletRequest;

import javax.servlet.http.HttpServletRequest;
import javax.validation.ConstraintValidator;
import javax.validation.ConstraintValidatorContext;


/**
 * @author 罗亚龙
 * @date 2021/9/25 9:03
 */
public class FileNotEmptyConstraintValidatorWithHttpServletRequest implements ConstraintValidator<FileNotEmpty, HttpServletRequest> {
    String fieldName;
    @Override
    public boolean isValid(HttpServletRequest request, ConstraintValidatorContext constraintValidatorContext) {
        MultipartHttpServletRequest servletRequest = (MultipartHttpServletRequest) request;
        return !servletRequest.getFiles(fieldName).isEmpty();
    }

    @Override
    public void initialize(FileNotEmpty constraintAnnotation) {
        fieldName = constraintAnnotation.value();
    }
}
